﻿#define  _CRT_SECURE_NO_WARNINGS

#include "Date.h"


void Date::Print()const
{
	cout << _year << "-" << _month << "-" << _day << endl;
}
//全缺省只有在函数声明
Date::Date(int year ,int month, int day )
{
	_year = year;
	_month = month;
	_day = day;
	if (!CheackDate())
	{
		cout << "非法日期" << endl;
	}
}


////d+50
////对象消失，不能引用返回
//Date Date::operator+(int day)
//{
//	//拷贝构造
//	Date tmp(*this);
//	tmp._day = _day + day;
//	while (tmp._day > GetMonthDay(tmp._year, tmp._month))
//	{
//		tmp._day -=  GetMonthDay(tmp._year, tmp._month);
//		tmp._month ++;
//		if (tmp._month == 13)
//		{
//			tmp._month = 1;
//			tmp._year++;
//		}
//	}
//	return tmp;
//}
//Date& Date::operator+=(int day)
//{
//	*this = *this + day;
//	return *this;
//}

//d1+=50
//先实现+=再实现+，复合性价高
Date& Date::operator+=(int day)
{
	if (day < 0)
	{
		return *this -= ( - day);
	}
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		_month++;
		if (_month == 13)
		{
			_month = 1;
			_year++;
		}
	}
	return *this;
}

//d1+50
Date Date::operator+(int day)const
{
	Date tmp(*this);
	return tmp += day;
}

// d1 -= 
Date& Date::operator-=(int day)
{
	//d1 -= -5000
	if (day < 0)
	{
		return *this += (-day);
	}
	_day -= day;
	while (_day <= 0)
	{
		_month--;//获取下一个月天数
		if (_month == 0)
		{
			_month = 12;
			_year--;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}
//d1-50
Date Date::operator-(int day)const
{
	Date tmp(*this);
	tmp -= day;
	return tmp;
}
//比较逻辑只需要实现> = 其他都可复用
bool Date::operator==(const Date& d)const
{
	return _year == d._year && _month == d._month && _day == d._day;
}
//d1>d2
bool Date::operator>(const Date& d)const
{
	if (_year > d._year)
	{
		return true;
	}
	else if (_year == d._year && _month > d._month)
	{
		return true;
	}
	else if (_year == d._year && _month == d._month && _day > d._day)
	{
		return true;
	}
	else
	{
		return false;
	}
}

//d1>=d2
bool Date::operator>=(const Date& d)const
{
	return (*this > d) || (*this == d);
}

bool Date::operator<(const Date& d)const
{
	return !(*this >= d);
}
bool Date::operator<=(const Date& d)const
{
	return !(*this >d);
}

bool Date::operator!=(const Date& d)const
{
	return !(*this == d);
}

//前置++  ++d
Date& Date::operator++()
{
	*this += 1;
	return *this;
}
//后置++ d++
Date Date ::operator++(int)
{
	Date tmp = *this;
	*this += 1;
	return tmp;
}

//前置--
Date& Date::operator--()
{
	*this -= 1;
	return *this;
}
//后置--
Date Date:: operator--(int)
{
	Date tmp = *this;
	*this -= 1;
	return tmp;
}

//d1-d2//日期-日期
int Date::operator-(const Date& d)const
{
	//找大小日期
	int flag = 1;
	Date max = *this;
	Date min = d;
	if (max < min)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int n = 0;
	while (min != max)
	{
		++min;//对象前置++销毁更小
		n++;
	}
	return n*flag;
}

//cout d
ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
	return out;
}

bool Date::CheackDate()
{
	if (_day<1 || _day>GetMonthDay(_year, _month) || _month < 1 || _month>12)
	{
		return false;
	}
	else
	{
		return true;
	}
}



istream& operator>>(istream& in, Date& d)
{
again:
	do
	{
		cout << "请输入年月日:";
		in >> d._year >> d._month >> d._day;
		if (!d.CheackDate())
		{
			cout << "非法日期，请重新输入" << endl;
			goto again;
		}
		else
		{
			break;
		}
	} while (1);
	return in;
}